package com.example.androidmainshixun.view;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.MomentsAdapter;
import com.example.androidmainshixun.base.BaseFragment;
import com.example.androidmainshixun.entity.Moment;

import java.util.ArrayList;
import java.util.List;

public class MomentFragment extends BaseFragment implements View.OnClickListener {

    private RecyclerView recyclerView;
    private MomentsAdapter momentsAdapter;
    private List<Moment> momentList;
    private ImageButton btnPublish;
    private Button btnFollowed, btnLatest;
    private ImageView btnMyProfile;

    private static final int REQUEST_CODE_PUBLISH = 1;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_publish) {
            Intent intent = new Intent(getActivity(), PublishActivity.class);
            startActivityForResult(intent, REQUEST_CODE_PUBLISH);
        } else if (v.getId() == R.id.btn_follow) {
            loadFollowedMoments();
        } else if (v.getId() == R.id.btn_latest) {
            loadLatestMoments();
        } else if (v.getId() == R.id.btn_my_profile) {
            Intent intent = new Intent(getActivity(), UserProfileActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.moment_fragment;
    }

    @Override
    protected void initView() {
        recyclerView = mView.findViewById(R.id.rv_moments);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        momentList = new ArrayList<>();
        momentsAdapter = new MomentsAdapter(momentList);
        recyclerView.setAdapter(momentsAdapter);

        btnPublish = mView.findViewById(R.id.btn_publish);
        btnPublish.setOnClickListener(this);

        btnFollowed = mView.findViewById(R.id.btn_follow);
        btnFollowed.setOnClickListener(this);

        btnLatest = mView.findViewById(R.id.btn_latest);
        btnLatest.setOnClickListener(this);

        btnMyProfile = mView.findViewById(R.id.btn_my_profile);
        btnMyProfile.setOnClickListener(this);

        loadLatestMoments();
    }

    @Override
    protected void initListener() {
        // 初始化其他监听器（如果有）
    }

    private void loadFollowedMoments() {
        momentList.clear();
        momentList.add(new Moment("FollowedUser1", "5分钟前", "这是关注用户的动态内容。", R.drawable.f2));
        momentList.add(new Moment("FollowedUser2", "15分钟前", "这是另一个关注用户的动态内容。", R.drawable.f3));
        momentsAdapter.notifyDataSetChanged();
    }

    private void loadLatestMoments() {
        momentList.clear();
        momentList.add(new Moment("User1", "10分钟前", "这是一个示例动态内容。", R.drawable.f1));
        momentList.add(new Moment("User2", "20分钟前", "这是另一个动态内容。", R.drawable.f4));
        momentsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PUBLISH && resultCode == getActivity().RESULT_OK && data != null) {
            String content = data.getStringExtra("content");
            ArrayList<String> selectedImages = data.getStringArrayListExtra("selectedImages");

            // 处理发布的内容和选中的图片
            if (selectedImages != null && !selectedImages.isEmpty()) {
                for (String imagePath : selectedImages) {
                    // 将每张图片添加为一个新的动态
                    Moment newMoment = new Moment("我", "刚刚", content, R.drawable.nong_you_quan_ok); // 示例图片资源
                    momentList.add(0, newMoment); // 添加到列表顶部
                }
            } else {
                // 如果没有选中图片，只添加文本内容
                Moment newMoment = new Moment("我", "刚刚", content, R.drawable.nong_you_quan_ok); // 示例图片资源
                momentList.add(0, newMoment); // 添加到列表顶部
            }
            momentsAdapter.notifyDataSetChanged(); // 通知适配器数据已更改
        }
    }
}